//
//  TTotheCallTextInputView.m
//  TaTogether
//
//  Created by 九月 on 11/8/24.
//

#import "TTotheCallTextInputView.h"


@interface TTotheCallTextInputView () <UITextFieldDelegate>

@property (nonatomic ,strong) UIButton *ttotheSendButton;


@end

@implementation TTotheCallTextInputView

- (instancetype)init
{
    if (self = [super init]) {


        self.backgroundColor = UIColor.whiteColor;
        
        UILabel *ttotheLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 30)];
        ttotheLabel.textColor = UIColor.whiteColor;
        ttotheLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        ttotheLabel.textAlignment = NSTextAlignmentCenter;
        ttotheLabel.text = @"请勿发送微信/QQ，谨防受骗，违者封号";
        ttotheLabel.backgroundColor = [UIColor colorWithRed:1 green:165/255.0 blue:74/255.0 alpha:1];
        [self addSubview:ttotheLabel];
        
        UIButton *ttotheBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ttotheBtn addTarget:self action:@selector(ttotheOnSendTextMessage) forControlEvents:UIControlEventTouchUpInside];
        ttotheBtn.enabled = NO;
        ttotheBtn.size =CGSizeMake(30, 30);
        ttotheBtn.left = self.width - ttotheBtn.width - 12;
        ttotheBtn.top = ttotheLabel.bottom + 10;
        [self addSubview:ttotheBtn];
        self.ttotheSendButton = ttotheBtn;
        
        _inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(12,ttotheBtn.top,ttotheBtn.left - 12 - 20,30)];
        _inputTextField.borderStyle = UITextBorderStyleNone;
        _inputTextField.delegate = self;
        _inputTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _inputTextField.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
        _inputTextField.backgroundColor = [UIColor clearColor];
        _inputTextField.returnKeyType = UIReturnKeySend;
        [_inputTextField addTarget:self action:@selector(changeTextFile:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_inputTextField];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillHideNotification object:nil];
        
        
        
    }
    return self;
}

- (void)ttotheOnSendTextMessage {}

// 键盘监听事件
- (void)keyboardAction:(NSNotification*)sender{
    // 通过通知对象获取键盘frame: [value CGRectValue]
    NSDictionary *useInfo = [sender userInfo];
    NSValue *value = [useInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    // <注意>具有约束的控件通过改变约束值进行frame的改变处理
    if([sender.name isEqualToString:UIKeyboardWillShowNotification]){
        
        [UIView animateWithDuration:0.2 animations:^{
            self.top = [[UIScreen mainScreen] bounds].size.height-self.height-[value CGRectValue].size.height;
        }];
    }
    else{
        [UIView animateWithDuration:0.2 animations:^{
            self.top = [[UIScreen mainScreen] bounds].size.height;
        }];
    }
}


- (void)changeTextFile:(UITextField *)textField {
    //这里搜索内容
    self.ttotheSendButton.enabled = textField.text.length;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_inputTextField.text.length) {
        [_inputTextField resignFirstResponder];
        [self sendTextMessage];
    }
    return YES;
}

- (void)sendButtonAction:(UIButton*)button
{
    button.enabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        button.enabled = YES;
    });
    if (_inputTextField.text.length) {
        [_inputTextField resignFirstResponder];
        [self sendTextMessage];
    }
}


- (void)sendTextMessage
{
    NSDate *datenow = [NSDate date];//
    NSInteger messageid = (long)([datenow timeIntervalSince1970]*1000);
    NSDictionary *paramDic = @{@"content":_inputTextField.text,@"selfId":[NSString stringWithFormat:@"%ld",(long)messageid],@"type":@(1),@"userId":self.ttotheUid,@"chargeFlag":@"true"};
    __weak typeof(self) fyweakSelf = self;
    
    fyweakSelf.inputTextField.text = @"";
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
